<template>
  <div class="tb-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch"></search-tool>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="75%"
      >
        <el-table-column label="序号" type="index" width="60" align="center" />
        <el-table-column prop="equipmentName" label="设备名称" align="center">
          <template #default="{ row }">
            <span>{{ row.equipmenmonitorForm.equipmentName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="equipmentNo" label="设备编号" align="center">
          <template #default="{ row }">
            <span>{{ row.equipmenmonitorForm.equipmentNo }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="pipeInfoName" label="所属管廊" align="center">
        </el-table-column>
        <el-table-column prop="warehouseName" label="所属仓室" align="center">
        </el-table-column>
        <el-table-column prop="subregionName" label="防火分区" align="center">
        </el-table-column>
        <el-table-column prop="monitoringItem" label="监控项" align="center">
          <template #default="{ row }">
            <span>{{ row.equipmenmonitorForm.monitoringItem }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="monitorStatus"
          label="监控状态（值）"
          align="center"
        >
          <template #default="{ row }">
            <span>{{ row.equipmenmonitorForm.monitorStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="updatedTime"
          label="更新时间"
          width="180"
          align="center"
        >
          <template #default="{ row }">
            <span>{{ row.updatedTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dealStatus" label="当前状态" align="center">
          <template #default="{ row }">
            <span>{{ row.equipmenmonitorForm.dealStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="action-row">
              <div>
                <div
                  v-show="
                    row.equipmenmonitorForm.dealStatus !== '误报' &&
                    row.equipmenmonitorForm.dealStatus !== '已处置'
                  "
                >
                  <el-tooltip content="分析处置" placement="top" effect="light">
                    <svg-icon
                      class="iconsty"
                      icon="run-product-disposal"
                      @click="handleAnalysis(row)"
                    ></svg-icon>
                  </el-tooltip>
                </div>
                <div
                  v-show="
                    row.equipmenmonitorForm.dealStatus === '误报' ||
                    row.equipmenmonitorForm.dealStatus === '已处置'
                  "
                >
                  <svg-icon
                    class="iconsty"
                    style="margin-right: 10px"
                    icon="product-forb"
                  ></svg-icon>
                </div>
              </div>
              <div>
                <div
                  v-show="
                    row.equipmenmonitorForm.dealStatus === '误报' ||
                    row.equipmenmonitorForm.dealStatus === '已处置'
                  "
                >
                  <el-tooltip content="解除" placement="top" effect="light">
                    <svg-icon
                      class="iconsty"
                      style="margin-right: 10px"
                      icon="run-product-relieve"
                      @click="handleAlarm(row)"
                    ></svg-icon>
                  </el-tooltip>
                </div>
                <div
                  v-show="
                    row.equipmenmonitorForm.dealStatus !== '已处置' &&
                    row.equipmenmonitorForm.dealStatus !== '误报'
                  "
                >
                  <svg-icon
                    class="iconsty"
                    icon="product-forbjc"
                    style="margin: 0 10px"
                  ></svg-icon>
                </div>
              </div>

              <div>
                <el-tooltip content="流程查看" placement="top" effect="light">
                  <svg-icon
                    class="iconsty"
                    icon="run-product-view"
                    @click="handleView(row)"
                  ></svg-icon>
                </el-tooltip>
              </div>
              <div class="has-more"></div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="启停状态" align="center">
          <template #default="{ row }">
            <el-switch
              v-model="row.startStopStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              @change="getStatus(row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <analysis-dialog
        :title="dialogTitle"
        :rowData="curRowData"
        :userInfo="userInfo"
        v-model:visible="visibleAnalysis"
        @onReload="reloadPage"
      ></analysis-dialog>
      <clear-alarm
        :title="dialogTitle"
        :rowData="curRowData"
        v-model:visible="visibleAlarm"
        @onReload="reloadPage"
      ></clear-alarm>
      <view-dialog
        :title="dialogTitle"
        :rowData="curRowData"
        :alarmNo="alarmNo"
        :srcList="srcList"
        :photoList="photoList"
        v-model:visible="visibleView"
        @onReload="reloadPage"
      ></view-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import AnalysisDialog from './components/AnalysisDialog/index.vue'
import ClearAlarm from './components/ClearAlarm/index.vue'
import ViewDialog from './components/ViewDialog/index.vue'
import { message } from '@/utils/message'
import {
  userList,
  equipmenmonitor,
  editStatus,
  equipmentQuery,
  photaQuery
} from '@/api/monitor.js'
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(8)
const params = ref({})
const loading = ref(false)
const visibleAnalysis = ref(false)
const visibleAlarm = ref(false)
const visibleView = ref(false)
const curRowData = ref({})
const dialogTitle = ref('')
const userInfo = ref({})
const alarmNo = ref('')
const rowStatus = ref('')
const equipmenmonitorForm = ref({})
const listData = ref({})
const rowList = ref({})
const srcList = ref([])
const photoList = ref([])
const getUserInfo = async () => {
  const res = await userList()
  userInfo.value = res
}
getUserInfo()

// 查询数据
const getListData = async (params) => {
  loading.value = true
  const res = await equipmenmonitor(params, page.value, size.value)
  tableData.value = res.rows
  equipmenmonitorForm.value = res.rows.equipmenmonitorForm
  total.value = res.total
  loading.value = false
}

const list = async (alarmNo) => {
  listData.value = await equipmentQuery(alarmNo)
  // console.log(listData.value.lift ? '1' : '2')
  rowList.value.dispose = listData.value.dispose
    ? listData.value.dispose
    : false
  if (rowList.value.dispose.id) {
    const res = await photaQuery(rowList.value.dispose.id)
    photoList.value = res
    res.forEach((item) => {
      srcList.value.push(item.file_url)
    })
  }
}

// 修改启停状态
const getStatus = async (row) => {
  const res = await editStatus(row.equipmenmonitorForm.equipmentNo)
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleAnalysis.value || visibleAlarm.value) {
    visibleAnalysis.value = false
    visibleAlarm.value = false
  }
}
reloadPage()

//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}
// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

const handleAnalysis = (row) => {
  dialogTitle.value = '分析处置'
  console.log(row)
  if (row.equipmenmonitorForm.dealStatus === '已解除') {
    message({ type: 'warning', message: '该条信息已解除，请勿重复点击' })
    // reloadPage()
  } else {
    curRowData.value = row
    visibleAnalysis.value = true
  }
}
const handleAlarm = (row) => {
  dialogTitle.value = '解除警报'
  curRowData.value = row
  visibleAlarm.value = true
}
const handleView = (row) => {
  dialogTitle.value = '全流程查看'
  curRowData.value = row
  list(row.equipmenmonitorForm.alarmNo)
  alarmNo.value = row.equipmenmonitorForm.alarmNo
  visibleView.value = true
}
</script>

<style lang="scss" scoped>
.tb-container {
  width: 100%;
  height: 100%;
  :deep(.el-table th.el-table__cell) {
    background-color: #e4edff;
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 30px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
.action-row {
  display: flex;
  .iconsty {
    cursor: pointer;
  }
  :deep(.svg-icon) {
    outline: 0;
  }
}
</style>
